﻿namespace 方法参数修饰符
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int x = 0, y = 0;
            Console.WriteLine($"x={x} y={y}");
            
            // 调用方式1
            TestOut(out x, out y);
            // 调用方式2
            TestOut(out int m, out int n);

            Console.WriteLine($"x={x} y={y}");
            Console.WriteLine($"m={m} n={m}");
            TestRef(ref x,ref y);
            Console.WriteLine($"x={x} y={y}");

            int sum = TestParams(1, 2, 3, 4);
            Console.WriteLine($"sum = {sum}");
            sum = TestParams();
            Console.WriteLine($"sum = {sum}");

            TestParams2(out sum, 1, 2, 3);
            Console.WriteLine($"sum = {sum}");

            Console.WriteLine("Hello, World!");
        }

        #region out
        /// <summary>
        /// 传递引用
        /// 可以返回多个值
        /// 参数可以不用赋初值
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void TestOut(out int a,out int b)
        {
            // 注意点，在这个方法中，必须要给 out 参数赋值
            a = 10;
            b = 20;
        }

        #endregion


        #region ref
        /// <summary>
        /// 传递的是引用
        /// 参数需要赋初值
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void TestRef(ref int a,ref int b)
        {
            a = 30;
            b = 40;
        }

        #endregion

        #region params
        /// <summary>
        /// 参数个数不确定，值传递，最后一个参数存在
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public static int TestParams(params int[] nums)
        {
            int sum = 0;
            foreach (int item in nums)
            {
                sum = sum + item;
            }
            return sum;
        }

        /// <summary>
        /// params 的参数必须是最后一个
        /// </summary>
        /// <param name="sum"></param>
        /// <param name="nums"></param>
        public static void TestParams2(out int sum,params int[] nums)
        {
            sum = 0;
            foreach (int item in nums)
            {
                sum = sum + item;
            }
        }

        #endregion

    }
}
